package cn.youyeku.zsxh.api;

import java.io.File;

import cn.youyeku.zsxh.Utils.LogUtils;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

public class XhApi {
    public final static String HOST = "http://www.youyeku.cn/";
    public final static String HOST_BASE = "www.youyeku.cn";
    private static final String API_VERSION = "api/v3/";// API版本

    public final static String BASE_URL = HOST + API_VERSION;

    public final static String API_REGISTER = "system/login/register.php";

    public final static String API_LOGIN = "system/login/login.php";

    public final static String API_ACTIVE = "system/account/active.php";

    public final static String API_GET_USER_INFO = "system/user/get_user_info.php";

    public final static String API_RESEND = "system/account/resend.php";

    public final static String API_ADD_FRIENDS = "system/user/add_get_friends.php";

//    public final static String API_GET_FRIENDS = "system/user/add_get_friends.php";

    public final static String API_GET_POST_HELPS = "system/help/get_post_helps.php";

    public final static String API_GET_POST_SHOPS = "system/shop/get_post_shops.php";

    public final static String API_CANCLE_FRIENDS = "system/user/cancel_friends.php";

    public final static String API_LOGIN_OUT = "system/account/loginout.php";

    public final static String API_POST_COMMENT = "system/shop/post_shop_comment.php";

    public final static String API_HELP_COMMENT = "system/user/help_comment.php";

    public final static String API_UPDATE_PROFILE = "system/account/update_profile.php";

    public final static String API_SEND_PRIVATE_MESSAGE = "system/user/private_message.php";

    public final static String API_POST_HELP_ACCEPT = "system/help/post_help_accept.php";
    public final static String API_GET_HELP_ACCEPTD = "system/help/get_help_accepted.php";

    public final static String API_GET_PRIVATE_MESSAGE = "system/user/private_message.php";

    public final static String API_GET_HELP_MESSAGE = "system/help/get_post_helps.php";


    public final static String API_GET_SHOP_CMT = "system/shop/get_shops_cmt.php";

    public final static String API_POST_QUESTION = "system/teacher/question_post.php";


    public final static String VERSION = BASE_URL + "system/new";
    public final static String API_POST_QUESTION_UPDATE="system/teacher/update_question_count.php";
    private static String API_POST_QUESTION_COUNT="system/teacher/question_count.php";

    public static void register(String mail, String nick_name, String passwd, Callback handler) {

        FormBody form = new FormBody.Builder()
                .add("mail", mail).add("nick_name", nick_name).add("passwd", passwd)
                .build();

        Request request = new Request.Builder().url(BASE_URL + API_REGISTER)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_REGISTER);
        AsyncHttpHelp.post(request, handler);
    }

    public static void login(String email, String password, Callback handler) {

        FormBody form = new FormBody.Builder()
                .add("email", email).add("password", password)
                .build();

        Request request = new Request.Builder().url(BASE_URL + API_LOGIN)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_LOGIN);
        AsyncHttpHelp.post(request, handler);
    }


    public static String getFace(String user_id) {

        LogUtils.w(BASE_URL + "uploads/" + user_id + "/face/" + user_id + ".jpg");
        return BASE_URL + "uploads/" + user_id + "/face/" + user_id + ".jpg";
    }

    public static String getShop(String user_id, String name) {
        LogUtils.w(BASE_URL + "uploads/" + user_id + "/shop/" + name);
        return BASE_URL + "uploads/" + user_id + "/shop/" + name;
    }

    public static void get_shop_cmt(String shop_id, Callback handler) {


        Request request = new Request.Builder().url(BASE_URL + API_GET_SHOP_CMT + "?shop_id=" + shop_id)
                .get()
                .build();

        LogUtils.i(BASE_URL + API_GET_SHOP_CMT + "?shop_id=" + shop_id);
        AsyncHttpHelp.post(request, handler);
    }

    public static void get_user_info(String uid, Callback handler) {

        FormBody form = new FormBody.Builder()
                .add("uid", uid)
                .build();

        Request request = new Request.Builder().url(BASE_URL + API_GET_USER_INFO)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_GET_USER_INFO);
        AsyncHttpHelp.post(request, handler);
    }

    public static void resend(Callback handler) {

        FormBody form = new FormBody.Builder()

                .build();

        Request request = new Request.Builder().url(BASE_URL + API_RESEND)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_RESEND);
        AsyncHttpHelp.post(request, handler);
    }

    public static void add_friends(String add_uid, Callback handler) {

        FormBody form = new FormBody.Builder()
                .add("add_uid", add_uid)
                .build();

        Request request = new Request.Builder().url(BASE_URL + API_ADD_FRIENDS)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_ADD_FRIENDS);
        AsyncHttpHelp.post(request, handler);
    }

    public static void get_friends(String title,Callback handler) {
        String API_GET_FRIENDS;
        if (title.equals("粉丝")){
            API_GET_FRIENDS ="system/user/get_fans.php";
        }else {
            API_GET_FRIENDS ="system/user/get_follows.php";
        }


        Request request = new Request.Builder().url(BASE_URL + API_GET_FRIENDS)
                .get()
                .build();

        LogUtils.i(BASE_URL + API_GET_FRIENDS);
        AsyncHttpHelp.get(request, handler);
    }

    public static void post_helps(String help_content, String help_address, String help_reward, Callback handler) {

        FormBody form = new FormBody.Builder()
                .add("help_content", help_content).add("help_address", help_address).add("help_reward", help_reward)
                .build();

        Request request = new Request.Builder().url(BASE_URL + API_GET_POST_HELPS)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_GET_POST_HELPS);
        AsyncHttpHelp.post(request, handler);
    }

    public static void get_helps(Callback handler) {


        Request request = new Request.Builder().url(BASE_URL + API_GET_HELP_MESSAGE)
                .get()
                .build();

        LogUtils.i(BASE_URL + API_GET_HELP_MESSAGE);
        AsyncHttpHelp.get(request, handler);
    }

    public static void get_shops(Callback handler) {

        Request request = new Request.Builder().url(BASE_URL + API_GET_POST_SHOPS)
                .get()
                .build();

        LogUtils.i(BASE_URL + API_GET_POST_SHOPS);
        AsyncHttpHelp.get(request, handler);
    }

    public static void cancle_friends(String del_uid, Callback handler) {

        FormBody form = new FormBody.Builder()
                .add("del_uid", del_uid)
                .build();

        Request request = new Request.Builder().url(BASE_URL + API_CANCLE_FRIENDS)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_CANCLE_FRIENDS);
        AsyncHttpHelp.post(request, handler);
    }

    public static void login_out(Callback handler) {

        FormBody form = new FormBody.Builder()

                .build();

        Request request = new Request.Builder().url(BASE_URL + API_LOGIN_OUT)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_LOGIN_OUT);
        AsyncHttpHelp.post(request, handler);
    }

    public static void post_comment(String cmt_text, String shop_id, Callback handler) {

        FormBody form = new FormBody.Builder()
                .add("cmt_text", cmt_text).add("shop_id", shop_id)
                .build();

        Request request = new Request.Builder().url(BASE_URL + API_POST_COMMENT)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_POST_COMMENT);
        AsyncHttpHelp.post(request, handler);
    }

    public static void help_comment(String cmt_text, String help_id, Callback handler) {

        FormBody form = new FormBody.Builder()
                .add("cmt_text", cmt_text).add("help_id", help_id)
                .build();

        Request request = new Request.Builder().url(BASE_URL + API_HELP_COMMENT)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_HELP_COMMENT);
        AsyncHttpHelp.post(request, handler);
    }

    public static void update_profile(String sign, String birthday, String sex, String face, Callback handler) {
        FormBody form = null;
        RequestBody requestBody= null;
        Request request;
        if (face.equals("")) {
            form = new FormBody.Builder()

                    .add("sign", sign)
                    .add("birthday", birthday)
                    .add("sex", sex)
                    .add("face", "face").build();


             request = new Request.Builder().url(BASE_URL + API_UPDATE_PROFILE)
                    .post(form)
                    .build();
        } else {


            String boundary = "------WebKitFormBoundaryBH3mhSkA4Li8k7Wy--";
            requestBody = new MultipartBody.Builder(boundary)
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("sign", sign)
                    .addFormDataPart("birthday", birthday)
                    .addFormDataPart("sex", sex)
                    .addFormDataPart("face", "face.jpg",
                            RequestBody.create(MediaType.parse("image/png"), new File(face)))


                    .build();

            request= new Request.Builder().url(BASE_URL + API_UPDATE_PROFILE)
                    .post(requestBody)
                    .build();
        }




        LogUtils.i(BASE_URL + API_UPDATE_PROFILE);
        AsyncHttpHelp.post(request, handler);
    }

    public static void send_private_message(String receiver_id, String message_content, Callback handler) {

        FormBody form = new FormBody.Builder()
              .add("receiver_id", receiver_id).add("message_content", message_content)
                .build();

        Request request = new Request.Builder().url(BASE_URL + API_SEND_PRIVATE_MESSAGE)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_SEND_PRIVATE_MESSAGE);
        AsyncHttpHelp.post(request, handler);
    }

    public static void post_help_accept(String help_id, Callback handler) {

        FormBody form = new FormBody.Builder()
                .add("help_id", help_id)
                .build();

        Request request = new Request.Builder().url(BASE_URL + API_POST_HELP_ACCEPT)
                .post(form)
                .build();

        LogUtils.i(BASE_URL + API_POST_HELP_ACCEPT);
        AsyncHttpHelp.post(request, handler);
    }


    public static void get_shop_accepted(Callback callback) {

    }

    public static void get_help_accepted(Callback handler) {


        Request request = new Request.Builder().url(BASE_URL+API_GET_HELP_ACCEPTD)
                .get()
                .build();
        AsyncHttpHelp.post(request, handler);
    }

    public static void post_shop(String shop_title, String shop_content , String file, Callback handler) {
        LogUtils.w(file);
        String boundary = "------WebKitFormBoundaryBH3mhSkA4Li8k7Wy--";
        RequestBody requestBody = new MultipartBody.Builder(boundary)
                .setType(MultipartBody.FORM)
                .addFormDataPart("shop_title", shop_title)
                .addFormDataPart("shop_content", shop_content)
                .addFormDataPart("shop_price", "2.3")
                .addFormDataPart("shop_pic", "SampleCropImage.jpg",
                        RequestBody.create(MediaType.parse("image/png"), new File(file)))

                .build();
        Request request = new Request.Builder().url(BASE_URL + API_GET_POST_SHOPS)
                .post(requestBody)
                .build();
        LogUtils.w(requestBody.toString());
        AsyncHttpHelp.post(request, handler);

    }


    public static void post_question(String test_name,String  question,Callback handler) {



        FormBody form = new FormBody.Builder()
                .add("question_desc", test_name)
                .add("question_data", question)
                .build();

        Request request = new Request.Builder().url(BASE_URL+API_POST_QUESTION)
                .post(form)
                .build();


        AsyncHttpHelp.post(request, handler);

    }

    public static void get_question(Callback handler) {

        Request request = new Request.Builder().url(BASE_URL+API_POST_QUESTION)
                .get()
                .build();


        AsyncHttpHelp.post(request, handler);

    }

    public static void get_question_count(Callback handler) {

        Request request = new Request.Builder().url(BASE_URL+API_POST_QUESTION_UPDATE)
                .get()
                .build();


        AsyncHttpHelp.post(request, handler);

    }

    public static void post_question_update(String question_id, String question_desc,String question_data,  Callback handler) {


        FormBody form = new FormBody.Builder()
                .add("question_data", question_data)
                .add("question_desc", question_desc)
                .add("question_id", question_id)
                .build();

        Request request = new Request.Builder().url(BASE_URL+API_POST_QUESTION_UPDATE)
                .post(form)
                .build();


        AsyncHttpHelp.post(request, handler);

    }

    public static void post_question_count(String que_count, String question_id,Callback handler) {

        FormBody form = new FormBody.Builder()
                .add("que_count", que_count)
                .add("question_id", question_id)
                .build();

        Request request = new Request.Builder().url(BASE_URL+API_POST_QUESTION_COUNT)
                .post(form)
                .build();


        AsyncHttpHelp.post(request, handler);

    }
}
